﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace test
{
    public partial class _Default : System.Web.UI.Page
    {
        SqlProcess sqlProc = new SqlProcess();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
            }
            
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            String OpName = DropDownList1.SelectedValue.ToString();
            OperationRule OPRule;
            DropDownList2.Items.Clear();
            DropDownList3.Items.Clear();
            DropDownList4.Items.Clear();

            try
            {
                OPRule = sqlProc.getOperationRule(OpName);

                /* parameter check */

                
                LabelCon1.Visible = false;
                LabelCon2.Visible = false;
                TextBoxCon1.Visible = false;
                TextBoxCon2.Visible = false;
                DropDownList3.Visible = false;
                DropDownList4.Visible = false;
            

                if (OPRule.nThreshold == 1)
                {
                    sqlProc.listingSensorOrDeviceID("PLACE", (object)DropDownList3);
                    LabelCon1.Text = OPRule.thresholdName1;
                    /* set visible true */
                    LabelCon1.Visible = true;
                    LabelCon2.Visible = false;

                    if (OPRule.thresholdName1.Equals("CONDITION"))
                    {
                        TextBoxCon1.Visible = true;
                        TextBoxCon2.Visible = false;
                    }
                    else
                    {
                        DropDownList3.Visible = true;
                        DropDownList4.Visible = false;
                    }


                }
                else if (OPRule.nThreshold == 2)
                {
                    sqlProc.listingSensorOrDeviceID("PLACE", (object)DropDownList3);
                    sqlProc.listingSensorOrDeviceID("BRIGHTNESS", (object)DropDownList4);
                    LabelCon1.Text = OPRule.thresholdName1;
                    LabelCon2.Text = OPRule.thresholdName2;

                    /* set visible true */
                    LabelCon1.Visible = true;
                    LabelCon2.Visible = true;

                    DropDownList3.Visible = true;

                    if (OPRule.thresholdName2.Equals("DURING"))
                    {
                        TextBoxCon2.Visible = true;
                    }
                    else
                    {
                        DropDownList4.Visible = true;
                    }
                }

                /* Sensor ID */
                if (OPRule.idType.Equals("SID"))
                {
                    sqlProc.listingSensorOrDeviceID("SENSOR", (Object)DropDownList2);

                }

                /* device ID */
                else if (OPRule.idType.Equals("DID"))
                {
                    sqlProc.listingSensorOrDeviceID("DEVICE", (Object)DropDownList2);
                }

                /* sensor + device ID */
                else if (OPRule.idType.Equals("ALL"))
                {
                    sqlProc.listingSensorOrDeviceID("SENSOR", (Object)DropDownList2);
                    sqlProc.listingSensorOrDeviceID("DEVICE", (Object)DropDownList2);

                }

                /* illumination_sensor */
                else if (OPRule.idType.Equals("STYPE"))
                {
                    DropDownList2.Items.Add("ILLUMINATION_SENSOR");
                    
                }


            }
            catch (Exception ex)
            {
                // ignore
            }
            finally
            {

            }   


        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ButtonADD_Click(object sender, EventArgs e)
        {
            String vocaName = TextBoxVOCA.Text;
            String op = DropDownList1.SelectedValue.ToString();
            String id = DropDownList2.SelectedValue.ToString();
            String param1="";
            String param2="";            
            String freq = TextBoxFreq.Text;

            if (TextBoxCon1.Visible == true)
            {
                param1 = TextBoxCon1.Text;
            }
            else if (DropDownList3.Visible == true)
            {
                param1 = DropDownList3.SelectedValue.ToString();
            }

            if (TextBoxCon2.Visible == true)
            {
                param2 = TextBoxCon2.Text;
            }
            else if (DropDownList4.Visible == true)
            {
                param2 = DropDownList4.SelectedValue.ToString();
            }

            Vocalbulary voca = new Vocalbulary();

            voca.vocaName = vocaName;
            voca.opName = op;
            voca.sensDevID = id;
            voca.param1 = param1;
            voca.param2 = param2;
            voca.freq = float.Parse(TextBoxFreq.Text);

            sqlProc.addVocabulary(voca);

            // rebind data on gridview
            GridView1.DataBind();

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Vocalbulary voca = new Vocalbulary();
            voca.vocaName = GridView1.SelectedRow.Cells[1].Text;
            voca.opName = GridView1.SelectedRow.Cells[2].Text;
            voca.sensDevID = GridView1.SelectedRow.Cells[3].Text;
            voca.param1 = GridView1.SelectedRow.Cells[4].Text;
            voca.param2 = GridView1.SelectedRow.Cells[5].Text;
            voca.freq = float.Parse(GridView1.SelectedRow.Cells[6].Text);


            TextBoxVOCA.Text = voca.vocaName;
            // change OP List
            changeSelectedItem((Object)DropDownList1, voca.opName);
            // change seleted item in Oplist
            DropDownList1_SelectedIndexChanged(null, null);


            // trinming &nbsp;
            if (voca.vocaName.Contains("&")) { voca.vocaName = ""; }
            if (voca.sensDevID.Contains("&")) { voca.sensDevID = ""; }
            if (voca.param1.Contains("&")) { voca.param1 = ""; }
            if (voca.param2.Contains("&")) { voca.param2 = ""; }


            // compare op rule
            OperationRule OPRule;
            OPRule = sqlProc.getOperationRule(voca.opName);

            //if not null voca.sensDevID, then change SensDev List (null string = &nbsp; )
            if (!voca.sensDevID.Equals(""))
            {
                changeSelectedItem((Object)DropDownList2, voca.sensDevID);
            }
            if (!voca.param1.Equals(""))
            {
                if (OPRule.thresholdName1.Equals("CONDITION"))
                {
                    TextBoxCon1.Text = voca.param1;
                }
                else
                {
                    changeSelectedItem((Object)DropDownList3, voca.param1);
                }
                
            }
            if (!voca.param2.Equals(""))
            {
                if (OPRule.thresholdName2.Equals("DURING") || OPRule.thresholdName2.Equals("BRIGHTNESS"))
                {
                    TextBoxCon2.Text = voca.param2;
                }
                else
                {
                    changeSelectedItem((Object)DropDownList4, voca.param2);
                }
                
            }

            TextBoxFreq.Text = voca.freq.ToString();




        }

        protected void ButtonDELETE_Click(object sender, EventArgs e)
        {
            String vocaName = TextBoxVOCA.Text;
            String op = DropDownList1.SelectedValue.ToString();
            String id = DropDownList2.SelectedValue.ToString();
            String param1 = DropDownList3.SelectedValue.ToString();
            String param2 = DropDownList4.SelectedValue.ToString();
            String freq = TextBoxFreq.Text;

            Vocalbulary voca = new Vocalbulary();

            voca.vocaName = vocaName;
            voca.opName = op;
            voca.sensDevID = id;
            voca.param1 = param1;
            voca.param2 = param2;
            voca.freq = float.Parse(TextBoxFreq.Text);
            try
            {
                sqlProc.deleteVocabulary(int.Parse(GridView1.SelectedRow.Cells[7].Text));
                // rebind data on gridview
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                
            }
            

            

        }



        /* when you select a item in List */
        private void changeSelectedItem(Object list, String name)
        {
            DropDownList DropDownListObj = (DropDownList)list;

            if (DropDownListObj.SelectedItem.Text.Equals(name) == false)
            {
                // check item
                if (DropDownListObj.Items.Contains(new ListItem(name)))
                {
                    // if exist get idx
                    int idx = DropDownListObj.Items.IndexOf(new ListItem(name));

                    // set all item not selected status
                    for (int i = 0; i < DropDownListObj.Items.Count; i++)
                    {
                        DropDownListObj.Items[i].Selected = false;
                    }
                    // select item what i want
                    DropDownListObj.Items[idx].Selected = true;
                    

                }
            }

        }

        protected void ButtonEDIT_Click(object sender, EventArgs e)
        {
            Vocalbulary voca = new Vocalbulary();
            voca.vocaName = TextBoxVOCA.Text;
            voca.opName = DropDownList1.SelectedValue.ToString();
            voca.sensDevID = DropDownList2.SelectedValue.ToString();
            voca.freq = float.Parse(TextBoxFreq.Text);

            OperationRule OPRule = sqlProc.getOperationRule(voca.opName);

            if (OPRule.thresholdName1.Equals("CONDITION"))
            {
                voca.param1 = TextBoxCon1.Text;
            }
            else
            {
                voca.param1 = DropDownList3.Text;
            }
            if (OPRule.thresholdName2.Equals("DURING") || OPRule.thresholdName2.Equals("BRIGHTNESS"))
            {
                voca.param2 = TextBoxCon2.Text;
            }
            else
            {
                voca.param2 = DropDownList4.Text;
            }


            sqlProc.editVocabulary(voca, int.Parse(GridView1.SelectedRow.Cells[7].Text));

            // rebind data on gridview
            GridView1.DataBind();
        }

        protected void ButtonRESET_Click(object sender, EventArgs e)
        {
            TextBoxCon1.Text = "";
            TextBoxCon2.Text = "";
            TextBoxFreq.Text = "";
            TextBoxVOCA.Text = "";

        }


    }

}
